<template>
  <div class="page-container">
    <div class="body-container">
      <div class="nav-container">
        <img :src="navImg" />
      </div>
      <div class="content-container">
        <div class="navlist-container">
          <ul>
            <li
              v-for="(item, index) of typeList"
              :key="index"
              @click="handleType(item.id)"
            >
              <span class="li-bc"></span>
              <span class="li-text">{{ item.typename }}</span>
            </li>
          </ul>
        </div>
        <div class="list-container">
          <div
            v-for="(item, index) of essayList"
            :key="index"
            class="essay-list  animate__animated animate__fadeInUp"
          >
            <div class="content-text">
              <div class="titleWrapper">
                <h2 @click="handleEssay(item.id)">
                  {{ item.title }}
                </h2>
              </div>
              <div class="introWrapper">{{ item.intro }}</div>
              <div class="infoWrapper">
                <div class="type-icon">
                  <i class="iconfont iconleixing"></i>
                  <span>{{ item.type.typename }}</span>
                </div>
                <div class="date-icon">
                  <i class="iconfont iconshijian"></i>
                  <span>{{ item.createdAt | formtTime }}</span>
                </div>
                <div class="target-icon" @click="handleEssay(item.id)">
                  <i class="iconfont iconhuaban1"></i>
                  <span>查看详情</span>
                </div>
              </div>
            </div>
            <div class="content-img">
              <img v-lazy="item.type.typeimg" />
            </div>
          </div>
        </div>
      </div>
      <div class="load-more" @click="loadMore" v-show="isMore"></div>
      <div class="load-more-none" v-show="isNone">--没有更多了--</div>
      <go-top :has-outline="false" :right="40"></go-top>
    </div>
  </div>
</template>

<script>
import GoTop from "@inotom/vue-go-top";
import ApiService from "@/service/index";
import { formtTime } from "@/utils/tools";
import { NAV, IMG_URL, NAV_PHOTO } from "@/config/config";

const api = new ApiService();
export default {
  name: "TypeViews",
  components: {
    GoTop
  },
  filters: {
    formtTime
  },
  async created() {
    this.originid = this.$route.query.originid;
    this.typeList = await api.findType();
    let typeid = this.originid;
    if (this.originid) {
      this.essayList = await api.findEssayType({ offset: 0, typeid });
      return;
    }
    this.essayList = await api.findEssayType({
      offset: 0,
      typeid: this.typeList[0].id
    });
    this.originid = this.typeList[0].id;
  },
  data() {
    return {
      originid: 0,
      navImg: NAV_PHOTO.type,
      offset: 0,
      isNone: false,
      isMore: true,
      typeList: [],
      essayList: []
    };
  },
  methods: {
    async loadMore() {
      this.offset += 5;
      let list = await api.findEssayType({
        offset: this.offset,
        typeid: this.originid
      });
      if (list.length <= 0) {
        this.isNone = true;
        this.isMore = false;
        return;
      }
      this.essayList.push(...list);
    },
    async handleType(typeid) {
      this.isNone = false;
      this.isMore = true;
      this.originid = typeid;
      this.essayList = await api.findEssayType({ offset: 0, typeid });
    },
    handleEssay(id) {
      this.$router.push({ path: "essay", query: { originid: id } });
    }
  }
};
</script>

<style lang="scss" scoped>
$theme: #f2db4d;
.body-container {
  width: 1305px;
  height: 100%;
  margin: 0 auto;
}
.nav-container {
  width: 103px;
  height: 94px;
  margin-top: 43px;
  img {
    width: 100%;
    height: 100%;
  }
}
.content-container {
  width: 100%;
  height: 100%;
  margin-top: 47px;
  .navlist-container {
    width: 295px;
    height: 100%;
    float: left;
    margin: 0;
    ul {
      list-style: none;
      margin-top: -40px;
      padding: 0;
      li {
        width: 295px;
        height: 71px;
        line-height: 71px;
        margin-top: 40px;
        position: relative;
        cursor: pointer;
        .li-bc {
          width: 17px;
          height: 100%;
          background-color: $theme;
          float: left;
          display: inline-block;
        }
        .li-text {
          display: inline-block;
          line-height: 71px;
          font-size: 16px;
          padding-left: 37px;
        }
      }
      li:after,
      li:before {
        content: "";
        position: absolute;
        left: 0;
        top: 0;
        width: 0;
        height: 100%;
        background: #fff;
        z-index: -2;
      }

      li:hover {
        z-index: 1;

        color: #ffffff;
      }

      li:before {
        transition: 0.5s;
        background: #f2db4d;
        z-index: -1;
      }
      li:hover:after,
      li:hover:before {
        width: 100%;
      }
    }
  }
  .list-container {
    margin-left: 434px;
    width: 871px;
    height: 100%;
  }
}
.essay-list {
  width: 871px;
  height: 172px;
  box-shadow: #999 0px 0px 10px;
  margin-top: 28px;
  .content-text {
    float: left;
    width: 594px;
    .titleWrapper {
      h2 {
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        cursor: pointer;
      }
      padding: 15px;
    }
    .introWrapper {
      font-size: 16px;
      margin-left: 35px;
    }
    .infoWrapper {
      padding-left: 120px;
      padding-top: 25px;
      position: relative;
      .type-icon {
        float: left;
        i {
          padding-right: 5px;
        }
      }
      .date-icon {
        margin-left: 30px;
        float: left;
        i {
          padding-right: 5px;
        }
      }
      .target-icon {
        float: left;
        position: absolute;
        right: 30px;
        top: 20px;
        width: 110px;
        height: 30px;
        line-height: 30px;
        text-align: center;
        border-radius: 9px;
        background-color: $theme;
        margin-left: 30px;
        color: #fff;
        cursor: pointer;
        i {
          padding-right: 5px;
        }
      }
    }
  }
  .content-img {
    float: left;
    width: 277px;
    height: 172px;
    overflow: hidden;
    img {
      width: 100%;
      height: 100%;
    }
  }
}
.load-more {
  box-sizing: border-box;
  width: 40px;
  height: 40px;
  margin-left: 810px;
  margin-top: 20px;
  border: 8px solid #dcdfe7;
  border-radius: 50%;
  transition: all 0.25s ease-in-out;
  cursor: pointer;
}
.load-more:hover {
  border-color: $theme;
  background-color: $theme;
  transform: scale(0.65);
}
.load-more-none {
  margin-left: 320px;
  text-align: center;
  margin-top: 20px;
}
</style>